package cz.proks.schoolpupilbook.shared.restlet;

import cz.proks.schoolpupilbook.shared.util.Tester;

public class RestMapping {	
	public static class LoginForm {
		public final static String PARAMETER_USERNAME = "username";
		public final static String PARAMETER_PASSWORD = "password";
	}
	
	public final static String URI_USER = "/rest/user";
	public final static String URI_COURSE = "/rest/course";
	public final static String URI_MARK = "/rest/mark";
	public final static String URI_USER_COURSE_RELATION = "/rest/userCourseRelation";
	public final static String URI_USER_RELATION = "/rest/userRelation";
	public final static String URI_LIST = "/rest/list";
	public final static String URI_MARK_COMMENT = "/rest/markComment";
	
	public final static String PARAMETER_USER_ID = "uId";
	public final static String PARAMETER_COURSE_ID = "cId";
	public final static String PARAMETER_MARK_ID = "mId";
	public final static String PARAMETER_USER_COURSE_RELATION_ID = "ucrId";
	public final static String PARAMETER_MARK_COMMENT_ID = "mcId";
	public final static String PARAMETER_USER_RELATION_ID = "urId";
	public final static String PARAMETER_USER_ROLE = "uRole";
	public final static String PARAMETER_LIST_ACTION = "action";
	private final static String PARAMETER_QUERY = "query";	
	
	public final static String REST_ROUTE_USER_BASE = buildRestUri(true, URI_USER, null);
	public final static String REST_ROUTE_USER = buildRestUri(true, URI_USER, PARAMETER_USER_ID);	
		
	public final static String REST_ROUTE_COURSE_BASE = buildRestUri(true, URI_COURSE, null);
	public final static String REST_ROUTE_COURSE = buildRestUri(true, URI_COURSE, PARAMETER_COURSE_ID);
		
	public final static String REST_ROUTE_MARK_BASE = buildRestUri(true, URI_MARK, null);
	public final static String REST_ROUTE_MARK = buildRestUri(true, URI_MARK, PARAMETER_MARK_ID);
	
	public final static String REST_ROUTE_USER_COURSE_RELATION_BASE = buildRestUri(true, URI_USER_COURSE_RELATION, null);
	public final static String REST_ROUTE_USER_COURSE_RELATION = buildRestUri(true, URI_USER_COURSE_RELATION, PARAMETER_USER_COURSE_RELATION_ID);
	
	public final static String REST_ROUTE_USER_RELATION_BASE = buildRestUri(true, URI_USER_RELATION, null);
	public final static String REST_ROUTE_USER_RELATION = buildRestUri(true, URI_USER_RELATION, PARAMETER_USER_RELATION_ID);
	
	public final static String REST_ROUTE_MARK_COMMENT_BASE = buildRestUri(true, URI_MARK_COMMENT, null);
	public final static String REST_ROUTE_MARK_COMMENT = buildRestUri(true, URI_MARK_COMMENT, PARAMETER_MARK_COMMENT_ID);
		
	public final static String REST_ROUTE_LIST_QUERY = buildRestUri(true, URI_LIST, PARAMETER_LIST_ACTION, PARAMETER_QUERY);
	public final static String REST_ROUTE_LIST_NONQUERY = buildRestUri(true, URI_LIST, PARAMETER_LIST_ACTION);
	
	public final static String METHOD_LIST_USERS = "users";
	public final static String METHOD_LIST_COURSES = "courses";
	public final static String METHOD_LIST_MARKS = "marks";
	public final static String METHOD_LIST_USER_RELATIONS = "userRelations";
	public final static String METHOD_LIST_USER_COURSE_RELATIONS = "userCourseRelations";
	public final static String METHOD_LIST_STUDENTS_FOR_COURSE = "studentsForCourse";
	public final static String METHOD_LIST_STUDENTS_FOR_PARENT = "studentsForParent";
	public final static String METHOD_LIST_COURSES_FOR_STUDENT = "coursesForStudent";
	public final static String METHOD_LIST_MARKS_FOR_COURSE_FOR_STUDENT = "marksForCourseForStudent";
	public final static String METHOD_LIST_USERS_BY_ROLE = "usersByRole";
	
	public static String buildRestUri(String uri, String... parameters) {
		return buildRestUri(false, uri, parameters);
	}
	
	private static String buildRestUri( boolean internal, String uri, String... parameters) {
		StringBuilder param = new StringBuilder();
		
		if (parameters != null)
			for (int i = 0; i < parameters.length; i++) {
				String p = parameters[i];
				String pathDelimiter = i == 0 ? "/" : i == 1 ? "?" : "&";
				if (Tester.isEmpty(p))
					continue;
				
				
				if (internal)
					param.append(pathDelimiter).append("{").append(p).append("}");
				else
					param.append(pathDelimiter).append(p);			
			}
		
		return uri + param.toString();
	}
	
}
